﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyFloridaHouse.libSTO;
using MyFloridaHouse.DataAccess;
using MyFloridaHouse.libSvDTO;

namespace MyFloridaHouse.libServiceBusinessRules
{
	/// <summary>
	/// Region Data methods
	/// </summary>
    public static class RegionData
    {
// this is the way to do it with table storage
        //public static LayerRegionsDataSTO GetLayerRegionsData(int targetLayerID, List<int> targetRegionIDList, List<DataDictionaryEntryDTO> sourceDictionaryFieldsList)
        //{

        //    //TODO:  Still need to handle case where all config layers show layer 0 for sourceLayerID

        //    LayerRegionsDataSTO results = new LayerRegionsDataSTO();
        //    results.regionsDataList = new List<RegionDataDTO>();
        //    try
        //    {

        //        DAL_CensusData dal = new DAL_CensusData();

        //        //this replaces the usp_tl10GetRegionsDataInView10 stored procedure with code.  

        //        // get all the target region data from the target layer and put in in the results
        //        // these data don't require an allocation because they are associated with the target regions.
        //        var targetLayerDictionaryIDList = (from field in sourceDictionaryFieldsList
        //                                            where field.layerID == targetLayerID
        //                                            select field.dictionaryID).ToList();
        //        foreach (var targetRegionID in targetRegionIDList)
        //        {
        //            var regionDataQuery = dal.GetRegionDataValues(targetLayerID, targetRegionID, targetLayerDictionaryIDList);
        //            results.regionsDataList.AddRange(regionDataQuery);
        //        }



        //        List<RegionDataDTO> regionAllocatedDataList = new List<RegionDataDTO>();
                
        //        // find all the different allocation layers requesed (i.e. sourceLayer != targetLayer)
        //        var sourceLayerList = (from field in sourceDictionaryFieldsList
        //                                  where field.layerID != targetLayerID
        //                                  select field.layerID).Distinct().ToList();
        //        foreach (var sourceLayerID in sourceLayerList)
        //        {
        //            // determine what data is being asked for from this source layer
        //            var sourceLayerDictionaryIDList = (from field in sourceDictionaryFieldsList
        //                                                  where field.layerID == sourceLayerID
        //                                                  select field.dictionaryID).ToList();

        //            foreach (var sourceLayerDictionaryID in sourceLayerDictionaryIDList)
        //            {

        //                foreach (var targetRegionID in targetRegionIDList)
        //                {
        //                    // get all the source regions that overlay the target region
        //                    // each source region will have a ratio of coverage to the target region
        //                    // we'll use that ratio to convert the source data to target data
        //                    var sourceRegionList = dal.GetRegionDataAllocations(sourceLayerID, targetLayerID, targetRegionID);

        //                    // now we know all the regions that cover the target region
        //                    // go get each covering region's data item, and sum them up
        //                    double dataValueSum = 0.0;
        //                    foreach (var sourceRegion in sourceRegionList)
        //                    {
        //                        // now get the data for this one sourceRegion that applies to the current target
        //                        RegionDataDTO sourceRegionData = dal.GetRegionDataValue(sourceLayerID, sourceRegion.sourceRegionID, sourceLayerDictionaryID);

        //                        // add the contribution of this source region to the target region's data value
        //                        dataValueSum += Convert.ToDouble(sourceRegionData.dataValue) * sourceRegion.allocationRatio;
        //                    }

        //                    RegionDataDTO newAllocatedRegionData = new RegionDataDTO();
        //                    newAllocatedRegionData.regionID = targetRegionID;
        //                    newAllocatedRegionData.sourceLayerID = sourceLayerID;
        //                    newAllocatedRegionData.dictionaryID = sourceLayerDictionaryID;
        //                    newAllocatedRegionData.dataValue = (Convert.ToInt32(dataValueSum)).ToString();
        //                }
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        results.errorMessage = ex.Message;
        //        return results;
        //    }
        //    return results;
        //}

        public static LayerRegionsDataSTO GetRegionListData(int iTargetLayerID, List<int> iRegionIDList, string iSourceLayerIDList, string iDictionaryIDList)
        {
            LayerRegionsDataSTO results = new LayerRegionsDataSTO();

            try
            {

                DAL_CensusData dal = new DAL_CensusData();
                var queryResults2 = dal.GetRegionListData(iTargetLayerID, iRegionIDList, iSourceLayerIDList, iDictionaryIDList);
                var z = from q in queryResults2
                        select q;
                results.regionsDataList = z.ToList();
                results.layerID = iTargetLayerID;
            }
            catch (Exception ex)
            {
                results.errorMessage = ex.Message;
                return results;
            }
            return results;
        }

        //TODO: Don't know which of these two ways to implement GetRegionsDataList is best

        // these were used with the SQL Server storage of RegionData, replaced with table storage; remove once table storage is working
        //        
        /// <summary>
        /// Gets the region data list.
        /// </summary>
        /// <param name="iLayerID">The ilayerID.</param>
        /// <param name="iRegionID">The iregionID.</param>
        /// <returns>Object: <see cref="RegionDataListSTO"/></returns>
        public static RegionDataListSTO GetRegionDataList(int iLayerID, int iRegionID)
        {
            RegionDataListSTO results = new RegionDataListSTO();
            results.layerID = iLayerID;
            try
            {
                DAL_CensusData dal = new DAL_CensusData();

                var queryResults = dal.GetRegionData(iLayerID, iRegionID);
                results.regionDataList = (from q in queryResults select q).ToList();
            }
            catch (Exception ex)
            {
                results.errorMessage = ex.Message;
                return results;
            }
            return results;
        }
        //public static RegionDataListSTO GetRegionDataList(int iLayerID, int iRegionID)
        //{
        //    RegionDataListSTO results = new RegionDataListSTO();
        //    results.layerID = iLayerID;
        //    try
        //    {
        //        DAL_CensusData dal = new DAL_CensusData();


        //        var regionDataQuery = dal.GetRegionDataValues(iLayerID, iRegionID);

        //        results.regionDataList = regionDataQuery.Select(item => new RegionDataNameAndValueDTO
        //        {
        //            dataValue = item.dataValue,
        //            fieldName = DAL_Global.GetDictionaryFieldName(item.sourceLayerID, item.dictionaryID)
        //        }
        //         ).ToList();

        //    }
        //    catch (Exception ex)
        //    {
        //        results.errorMessage = ex.Message;
        //        return results;
        //    }
        //    return results;
        //}
    }
}
